package pl.plesken.swt;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import pl.plesken.languages.SetLanguage;
import pl.plesken.tools.RunShowLogs;
import pl.plesken.xml.ConfigParser;

public class ShowLogs extends Dialog {

	private StyledText logStyledText;
	private ProgressBar progressBar;
	private Button okButton;
	protected Object result;
	protected Shell shell;
	StringBuffer log = new StringBuffer();
	StringBuffer run = new StringBuffer();
	StringBuffer lang = new StringBuffer();
	StringBuffer length = new StringBuffer();
	StringBuffer count = new StringBuffer();
	//String base = "java -Xms256m -Xmx512m -classpath .:./resources:./lib/moviejukebox.jar:./lib/commons-logging-1.1.1.jar:./lib/commons-lang-2.4.jar:./lib/commons-configuration-1.5.jar:./lib/commons-collections-3.2.1.jar:./lib/filters.jar:./lib/mucommanderlight.jar:./lib/thetvdbapi.jar:./lib/saxon9.jar com.moviejukebox.MovieJukebox";
	String dst;
	String skin;
	

	public String getDst() {
		return dst;
	}

	public void setDst(String dst) {
		this.dst = dst;
	}

	public String getSkin() {
		return skin;
	}

	public void setSkin(String skin) {
		this.skin = skin;
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public ShowLogs(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public ShowLogs(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		logs();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		ConfigParser cnf = new ConfigParser();
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(500, 375);
		shell.setText(SetLanguage.getString("ShowLogs.nameApp")); //$NON-NLS-1$
		
		lang.append(cnf.readConf("language")); //$NON-NLS-1$
		SetLanguage.setLang(lang.toString());
		
		logStyledText = new StyledText(shell, SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY | SWT.H_SCROLL | SWT.BORDER);
		logStyledText.setEditable(false);
		logStyledText.setJustify(true);
		logStyledText.setCapture(true);
		final GridData gd_logStyledText = new GridData(SWT.FILL, SWT.FILL, true, true);
		logStyledText.setLayoutData(gd_logStyledText);

		progressBar = new ProgressBar(shell, SWT.HORIZONTAL | SWT.SMOOTH);
		progressBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		progressBar.setMinimum(0);
		
		okButton = new Button(shell, SWT.NONE);
		okButton.setEnabled(false);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.close();
			}
		});
		final GridData gd_okButton = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		gd_okButton.widthHint = 75;
		okButton.setLayoutData(gd_okButton);
		okButton.setText(SetLanguage.getString("ShowLogs.end")); //$NON-NLS-1$
	}
	
	private void logs() {
		Thread thread = new Thread() {
			public void run() {
				try {
					RunShowLogs sh = new RunShowLogs();
					
					sh.setDst(dst);
					
					Process p = Runtime.getRuntime().exec(sh.runSystem()); //$NON-NLS-1$ //$NON-NLS-2$
					
					BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getErrorStream()));
					String s;

					length.setLength(0);
					
					while((s = stdInput.readLine()) != null) { 
						final String a = s;
						Display.getDefault().syncExec(new Runnable() {
							public void run() {
								RunShowLogs sh = new RunShowLogs();
								System.out.println(a);
								logStyledText.append(a + "\n"); //$NON-NLS-1$

								if (!sh.findLength(a).equals("")) { //$NON-NLS-1$
									length.append(sh.findLength(a));
								}

								if (sh.findMovie(a) == true) {
									if (count.toString().equals("")) { //$NON-NLS-1$
										count.append("0"); //$NON-NLS-1$
									} 

									if (!length.toString().equals("")) { //$NON-NLS-1$
										int size = Integer.valueOf(length.toString());
										if (size > 0) {
											progressBar.setMaximum(size);
											int item = Integer.valueOf(count.toString()) + 1;
											progressBar.setSelection(item);
											int ntm = Integer.valueOf(count.toString()) + 1;
											count.setLength(0);
											count.append(ntm);
										}
									}
								}
								
							}
						});
					}

					stdInput.close();
					
					if (skin.toLowerCase().contains("Unique_BGL")) {
						
					}
					
					Display.getDefault().syncExec(new Runnable() {
						public void run() {
							MessageBox end = new MessageBox(shell, SWT.ICON_INFORMATION);
							end.setMessage(SetLanguage.getString("ShowLogs.messageInfoText"));  //$NON-NLS-1$
							end.setText(SetLanguage.getString("ShowLogs.messageInfoTopic"));   //$NON-NLS-1$
							end.open();
							okButton.setEnabled(true);
						}
					});
			
				} catch(Exception e1){
				}
			}
		};
		thread.start();
	}

}
